package com.xmg.boot.demo.bootlesson1.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmg.boot.demo.bootcommon.utils.page.*;
import com.xmg.boot.demo.bootlesson1.user.entity.pojo.User;
import com.xmg.boot.demo.bootlesson1.user.mapper.UserMapper;
import com.xmg.boot.demo.bootlesson1.user.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ma
 * @date 2023/11/16
 * @description
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Page<User> getUserByPage(String userName,int currentIndex,int pageSize) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.lambda().like(User::getUserName,userName);
        Page<User> userPage = new Page<>(currentIndex, pageSize);
        return this.page(userPage,userQueryWrapper);
    }

    @Override
    public PageResult<User> getCommonMethod(String userName, int currentIndex, int pageSize) {
        //第二种分页，直接调用方法
        PagingOptions options=new PagingOptions();
        options.setPageIndex(currentIndex);
        options.setPageSize(pageSize);
        List<PagingFilter> filters=new ArrayList<>();
        filters.add(new PagingFilter("user_name","cn",userName));
        List<PagingSort> sorts=new ArrayList<>();
        sorts.add(new PagingSort("id","asc"));
        options.setFilters(filters);
        options.setSorts(sorts);

        return PageResult.getPage(new PageQuery(options,this.baseMapper),this.baseMapper);
    }
}
